var app = getApp();
Page({
  data: {
    time: "",
    username: "",
    list1: [],
    list2: [],
    list3: [],
    searVal:'',
    true: "true",
    data_url: "",
    user_search: "",
    in_lunbo: [],
    iv: "",
    userInfo: {},
    encryptedData: "",
    session_key: "",
    session_id: "",
    lat: 0,
    long: 0,
    showCupponDialog:false
  },
  // 关闭模态框
  toggleShowCupponDialog(){
    let that=this
    that.setData({
      showCupponDialog: !that.data.showCupponDialog
    })
  },
  // 轮播图转跳
  bannerJump: function (e) {
    if (e.currentTarget.dataset.url == '/pages/distribution/distribution') {
      app.getSessionId().then(function () {
        wx.navigateTo({
          url: e.currentTarget.dataset.url + '?sharekey=' + app.globalData.sharekey,
        })
      })
    } else {
      wx.navigateTo({
        url: e.currentTarget.dataset.url,
      })
    }
  },
  comSearch: function (e) {
    wx.navigateTo({
      url: '/pages/com_search/com_search?id=' + e.detail.value,
    })
  },
  clear:function(){
    this.setData({
      searVal:''
    })
  },
  // 跳转优惠券页面
  jumpToCoupon(e){
    wx.navigateTo({
      url: '/pages/coupon/coupon?type='+e.currentTarget.dataset.coupon,
    })
  },
  onLoad: function (options) {
    // 传递指向对象
    var that = this
    app.buyCar = [],
      app.buyCar1 = []
    this.setData({
      data_url: app.globalData.data_url
    })
    //获取定位
    wx.getLocation({
      type: "gcj02",
      success: function (res) {
        that.setData({
          long: res.longitude,
          lat: res.latitude
        })
      }
    })
    // 获取轮播图
    wx.request({
      url: that.data.data_url + '/GPD/getCars.do',
      data:{
        type:1
      },
      success: function (res) {
        that.setData({
          in_lunbo: res.data
        })
      }
    }),
    // 抓取数据
    // 获取热卖商品
    wx.request({
      url: that.data.data_url + '/GPD/getHotSellingGoods.do',
      header: {
        'Content-Type': 'application/json'
      },
      success: function (res) {
        that.setData({
          list1: res.data.map(function (item) {
            item.price = item.price.toFixed(2)
            return item
          })
        })
      }
    }),
    // 获取新品上市
    wx.request({
    url: that.data.data_url + '/GPD/getnewProduct.do',
      header: {
        'Content-Type': 'application/json'
      },
      success: function (res) {
        that.setData({
          list2: res.data.map(function (item) {
            item.price = item.price.toFixed(2)
            return item
          })
        })
      }
    }),
    // 获取限时折扣
    wx.request({
      url: that.data.data_url + '/GPD/getfindByTimeLimit.do',
      success: function (res) {
        that.setData({
          list3: res.data.map(function(item){
            item.price = item.price.toFixed(2)
            return item
          })
        })
      }
    })
    // 获取优惠券弹框
    app.getSessionId().then(function(sessionId){
      wx.request({
        url: that.data.data_url + '/GPD/getIndexCoupon.do',
        data: {
          session_id: sessionId
        },
        success: function (res) {
          that.setData({
            showCupponDialog: res.data.coupons.length != 0
          })
        }
      })
    })
  },
  jumpDetails: function (e) {
    wx.navigateTo({
      url: '/pages/details/details?id=' + e.currentTarget.dataset.id
    })
  },
  jumpMore: function (e) {
    wx.navigateTo({
      url: '/pages/more/more?text=' + e.currentTarget.dataset.id
    })
  },
  jumpJINXUANSHANGIA:function(){
    wx.navigateToMiniProgram({
      appId: 'wx420f6746cd1790ec',
    })
  }
})    